package com.xoa.util.cms.directive;

import com.xoa.dao.portalArticle.PortalArticleMapper;
import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.dao.portalTemplate.PortalTemplateMapper;
import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.portalArticle.PortalArticleWithBLOBs;
import com.xoa.model.portalColumn.PortalColumn;
import com.xoa.model.portalTemplate.PortalTemplateWithBLOBs;
import com.xoa.model.cms.Site;
import com.xoa.service.cms.RenderingService;
import com.xoa.util.common.StringUtils;
import freemarker.core.Environment;
import freemarker.template.*;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class CMSDocumentDirective implements TemplateDirectiveModel {
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        //从标签获取文章ID
        int articleId = StringUtils.getInteger(map.get("articleId"),0);
        // 站点ID
        int siteId = StringUtils.getInteger(environment.getCustomAttribute("SITE_ID"),0);

        //从上下文中获取文章ID
        int content_articleID = StringUtils.getInteger(environment.getCustomAttribute("articleId"),0);

        Writer w = environment.getOut();

        boolean preview = (boolean) environment.getCustomAttribute("preview");

        //获取bean
        PortalArticleMapper portalArticleMapper = (PortalArticleMapper)environment.getCustomAttribute("portalArticleMapper");
        PortalColumnMapper portalColumnMapper = (PortalColumnMapper) environment.getCustomAttribute("portalColumnMapper");
        SiteMapper siteMapper = (SiteMapper) environment.getCustomAttribute("siteMapper");
        PortalTemplateMapper portalTemplateMapper = (PortalTemplateMapper) environment.getCustomAttribute("portalTemplateMapper");
        RenderingService renderingService = (RenderingService) environment.getCustomAttribute("renderingService");


        //保存上层变量
        Object loopObj = environment.getCustomAttribute("loopObj");
        TemplateModel this0 = environment.getVariable("this");
        Map data = new HashMap();
        PortalArticleWithBLOBs p = null;
        if(articleId!=0){
            //从标签获取文章id
            p = portalArticleMapper.selectByPrimaryKey(articleId);
        }else{
            p = portalArticleMapper.selectByPrimaryKey(content_articleID);
        }

        //如果没有查询到对应的文章直接返回
        if(p==null){
            return;
        }

        environment.setCustomAttribute("docId", p.getArticleId());


        //获取到栏目信息
        PortalColumn channelInfo = portalColumnMapper.selectByPrimaryKey(StringUtils.getInteger(p.getColumnId(), 0));
        PortalTemplateWithBLOBs st = portalTemplateMapper.selectByPrimaryKey(channelInfo.getDetailTemplate());
        Site siteInfo = siteMapper.selectByPrimaryKey(siteId);


        String absPath = renderingService.getCloumnAbsolutePath(channelInfo.getColumnId());

        if(st!=null){
            data.put("ABS_PATH", absPath+"/"+p.getArticleId()+"."+siteInfo.getPubFileExt());//绝对路径
            int index = absPath.indexOf("/", 2);
            absPath = absPath.substring(index+1);
            data.put("REL_PATH", environment.getCustomAttribute("ROOT_PATH")+absPath+"/"+p.getArticleId()+"."+siteInfo.getPubFileExt());//相对路径
        }

        //复制类容
        data.put("content",p.getArticleComtent());
        data.put("title",p.getArticleTitle());
        data.put("createUserName",p.getCreateUserName());
        data.put("mainHead",p.getMainHead());
        data.put("subHead",p.getSubHead());
        data.put("keyWord",p.getKeyWord());
        data.put("articleSummary",p.getArticleSummary());
        data.put("articleSource",p.getArticleSource());
        data.put("author",p.getAuthor());
        data.put("thumbnailName",p.getThumbnailName());
        data.put("attachmentName",p.getAttachmentName());


        //处理日期
        data.put("writeTimeDesc", p.getAuthorTime());
        data.put("crTimeDesc", p.getCreateTime());


        if(!preview){
            //处理内容中的图片
        }
        //如果是预览中
        if(preview){
            data.put("ABS_PATH", "/cmsPub/preview?siteId="+siteId+"&channelId="+channelInfo.getColumnId()+"&chnlDocId="+p.getArticleId());//绝对路径
            data.put("REL_PATH", "/cmsPub/preview?siteId="+siteId+"&channelId="+channelInfo.getColumnId()+"&chnlDocId="+p.getArticleId());//相对路径
        }

        Map _this = new HashMap();
        _this.put("instance", data);
        environment.setVariable("this", new DefaultObjectWrapper().wrap(_this));
        environment.setCustomAttribute("loopObj", data);

        if(templateDirectiveBody!=null){
            templateDirectiveBody.render(w);
        }
        environment.setVariable("this", this0);
        environment.setCustomAttribute("loopObj", loopObj);




    }
}
